package com.examplemartin.jpademo.controller.admin;

import com.examplemartin.jpademo.bean.AjaxResult;
import com.examplemartin.jpademo.pojo.admin.OperatorLogPO;
import com.examplemartin.jpademo.pojo.po.RolePO;
import com.examplemartin.jpademo.pojo.po.UserPO;
import com.examplemartin.jpademo.pojo.po.stock.StockPO;
import com.examplemartin.jpademo.service.StockService;
import com.examplemartin.jpademo.service.UserService;
import com.examplemartin.jpademo.service.admin.MenuService;
import com.examplemartin.jpademo.service.admin.OperatorLogService;
import com.examplemartin.jpademo.service.admin.RoleService;
import com.examplemartin.jpademo.util.SessionUtil;
import com.examplemartin.jpademo.util.ValidateMember;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("/user")
@Controller
@Slf4j
public class UserController {

    @Resource
    private MenuService menuService;
    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;
    @Resource
    private OperatorLogService operatorLogService;

    @Resource
    private StockService stockService;

    /**
     * 跳转到user主页面
     *
     * @return
     */
    @GetMapping("/userlist.do")
    public String rolelist(Model model) {
        model.addAttribute("title", "用户管理");

        List<UserPO> userlist = userService.findUser();

        model.addAttribute("userlist", userlist);

        List<StockPO> allstock = stockService.findAll();
        model.addAttribute("stocklist", allstock);

        return "user/userlist";
    }

    @GetMapping("/adduser.do")
    public String addUserDo(Model model) {
        model.addAttribute("title", "添加用户");
        List<RolePO> allRole = roleService.findAll();
        model.addAttribute("rolelist", allRole);
        List<StockPO> allstock = stockService.findAll();
        model.addAttribute("stocklist", allstock);

        return "user/adduser";
    }

    /**
     * ajax 添加新用户
     *
     * @param userPO
     *
     * @return
     */
    @PostMapping("/adduser.ajax")
    @ResponseBody
    public AjaxResult addUserAjax(@RequestBody UserPO userPO, HttpServletRequest request) {

        //验证userPO -- 登入名
        if (ValidateMember.isEmpty(userPO.getLoginname())) {
            return AjaxResult.fail(500, "必须填写登入名");
        }
        //验证userPO -- 姓名
        if (ValidateMember.isEmpty(userPO.getUsername())) {
            return AjaxResult.fail(500, "必须提供姓名");
        }

        //验证userPO -- 密码
        if (ValidateMember.isEmpty(userPO.getPassword())) {
            return AjaxResult.fail(500, "必须提供密码");
        }

        //验证userPO -- 角色
        if (ValidateMember.isEmpty(userPO.getRole())) {
            return AjaxResult.fail(500, "必须提供角色");
        }

        //验证userPO -- 状态
        if (ValidateMember.isEmpty(userPO.getStatus())) {
            return AjaxResult.fail(500, "必须提供状态");
        }

        Integer count = userService.countUserByLoginName(userPO.getLoginname());
        if (count != 0) {
            return AjaxResult.fail(500, "登入名已存在");
        }

        //

        UserPO result = userService.insertUser(userPO);

        //操作日志
        // 判断 session和loginuserr
        HttpSession session = request.getSession();
        if (ValidateMember.isEmpty(session)) {
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }
        UserPO loginuser = (UserPO) session.getAttribute("loginuser");

        if (ValidateMember.isEmpty(loginuser)) {
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }


        OperatorLogPO logPO = new OperatorLogPO();
        logPO.setCreateTime(new Date());


        UserPO currentUser = SessionUtil.getLoninUser();
        if (ValidateMember.isEmpty(currentUser)) {
            return AjaxResult.fail(500, "非法用户,请联系管理员");
        }

        logPO.setUser(currentUser);


        if (result == null) {

            logPO.setContent("新用户: <" + userPO.getUsername() + "> 创建失败!!!!!!!!!!!!!");
            operatorLogService.saveLog(logPO);
            return AjaxResult.fail(500, "新用户: <" + userPO.getUsername() + "> 创建失败!!!!!!!!!!!!!");
        } else {
            logPO.setContent("新用户: <" + userPO.getUsername() + "> 成功创建");
            operatorLogService.saveLog(logPO);
            return AjaxResult.success(200, "新用户: <" + userPO.getUsername() + "> 成功创建", null);
        }

    }

    /**
     * 跳转到修改user页面
     *
     * @return
     */
    @GetMapping("/edituser.do")
    public String editUserDo(Long id, Model model) {
        model.addAttribute("title", "修改用户");

        List<RolePO> allRole = roleService.findAll();
        model.addAttribute("rolelist", allRole);

        UserPO userById = userService.findUserById(id);
        model.addAttribute("userPO", userById);

        List<StockPO> allstock = stockService.findAll();
        model.addAttribute("stocklist", allstock);


        return "user/edituser";
    }

    /**
     * ajax 修改用户
     *
     * @param userPO
     *
     * @return
     */
    @PostMapping("/edituser.ajax")
    @ResponseBody
    public AjaxResult saveUserAjax(@RequestBody UserPO userPO, HttpServletRequest request) {

        //验证userPO -- id
        if (ValidateMember.isEmpty(userPO.getId())) {
            return AjaxResult.fail(500, "用户非法");
        }
        //验证userPO -- 登入名
        if (ValidateMember.isEmpty(userPO.getLoginname())) {
            return AjaxResult.fail(500, "必须填写登入名");
        }
        //验证userPO -- 姓名
        if (ValidateMember.isEmpty(userPO.getUsername())) {
            return AjaxResult.fail(500, "必须提供姓名");
        }

        //验证userPO -- 密码
        if (ValidateMember.isEmpty(userPO.getPassword())) {
            return AjaxResult.fail(500, "必须提供密码");
        }

        //验证userPO -- 角色
        if (ValidateMember.isEmpty(userPO.getRole())) {
            return AjaxResult.fail(500, "必须提供角色");
        }

        //验证userPO -- 状态
        if (ValidateMember.isEmpty(userPO.getStatus())) {
            return AjaxResult.fail(500, "必须提供状态");
        }

        UserPO userById = userService.findUserById(userPO.getId());
        if(userById==null){
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }

        userById.setStatus(userPO.getStatus());
        userById.setUpdateTime(new Date());
        userById.setRole(userPO.getRole());
        userById.setUsername(userPO.getUsername());
        userById.setComment(userPO.getComment());
        userById.setPhone(userPO.getPhone());
        userById.setEmail(userPO.getEmail());
        userById.setStockPO(userPO.getStockPO());


        UserPO result = userService.insertUser(userPO);

        //操作日志
        // 判断 session和loginuserr
        HttpSession session = request.getSession();
        if (ValidateMember.isEmpty(session)) {
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }
        UserPO loginuser = (UserPO) session.getAttribute("loginuser");
        if (ValidateMember.isEmpty(loginuser)) {
            return AjaxResult.fail(500, "有非法操作,请联系管理员");
        }

        OperatorLogPO logPO = new OperatorLogPO();
        logPO.setCreateTime(new Date());
        /*logPO.setContent("成功删除菜单:" + menuById.getMenuname());*/


        UserPO currentUser = userService.login(loginuser.getLoginname(), loginuser.getPassword());

        logPO.setUser(currentUser);


        if (result == null) {

            logPO.setContent("用户: <" + userPO.getUsername() + "> 修改失败!!!!!!!!!!!!!");
            operatorLogService.saveLog(logPO);
            return AjaxResult.fail(500, "用户: <" + userPO.getUsername() + "> 修改失败!!!!!!!!!!!!!");
        } else {
            logPO.setContent("用户: <" + userPO.getUsername() + "> 成功修改");
            operatorLogService.saveLog(logPO);
            return AjaxResult.success(200, "用户: <" + userPO.getUsername() + "> 成功修改", null);
        }

    }

}
